package com.fare.taluo.application.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dayatang.querychannel.service.QueryChannelService;
import com.dayatang.querychannel.support.Page;
import com.fare.taluo.application.ExcelInfoApplication;
import com.fare.taluo.core.domain.analyse.report.download.ExcelInfo;
import com.fare.taluo.vo.core.ExcelInfoVO;

@Transactional
@Scope("singleton")
@Service("excelInfoApplicationImpl")
public class ExcelInfoApplicationImpl implements ExcelInfoApplication {
	
	@Autowired
	public  QueryChannelService queryChannel1;
	
	@Override
	public List<ExcelInfoVO> findAll() {
		List<ExcelInfo> listEntity = new ArrayList<ExcelInfo>();
		List<ExcelInfoVO> listVO = new ArrayList<ExcelInfoVO>();
		ExcelInfo excelInfo =new ExcelInfo();
		listEntity =excelInfo.findAll();
		if(listEntity!=null && listEntity.size()>0){
			for(ExcelInfo entity :listEntity){
				ExcelInfoVO vo =new ExcelInfoVO();
				vo.setId(entity.getId());
				vo.setCode(entity.getCode());
				vo.setDateType(entity.getDateType());
				vo.setName(entity.getName());
				vo.setPath(entity.getPath());
				vo.setUploadDate(entity.getUploadDate());
				listVO.add(vo);
			}
		}
	
		return listVO;
	}

	@Override
	public void saveOrUpdate(ExcelInfoVO excelInfovo) {
		
		ExcelInfo entity =new ExcelInfo();
		entity.setCode(excelInfovo.getCode());
		entity.setDateType(excelInfovo.getDateType());
		entity.setUploadDate(excelInfovo.getUploadDate());
		entity.setName(excelInfovo.getName());
		entity.setPath(excelInfovo.getPath());
		entity.saveOrUpdate(entity);

	}

	@Override
	public void delete(ExcelInfoVO excelInfovo) {
		ExcelInfo entity =ExcelInfo.get(ExcelInfo.class, excelInfovo.getId());
		entity.delete(entity);

	}

	@Override
	public ExcelInfoVO findById(Long id) {
		ExcelInfo entity =ExcelInfo.get(ExcelInfo.class, id);
		ExcelInfoVO vo =new ExcelInfoVO();
		vo.setId(entity.getId());
		vo.setCode(entity.getCode());
		vo.setDateType(entity.getDateType());
		vo.setName(entity.getName());
		vo.setPath(entity.getPath());
		vo.setUploadDate(entity.getUploadDate());
	
		return vo;
	}

	@Override
	public Page<ExcelInfoVO> pageForExcelInfo(String code, int pageNo,
			int pageSize) {
		List<ExcelInfoVO> listvo =new ArrayList<ExcelInfoVO>();
		StringBuffer hql = new StringBuffer();
		List<Object> params = new ArrayList<Object>();
		hql.append(" from ExcelInfo where id  in(select min(id) from ExcelInfo e group by e.dateType)");
		if(code!=null){
			hql.append(" and name = ? ");
			params.add(code);
		}
		hql.append(" order by uploadDate desc ");
		
		Page<ExcelInfo> page = queryChannel1.queryPagedResultByPageNo(hql.toString(), params.toArray(), pageNo, pageSize);
		if(page.getTotalCount()>0){
			for(ExcelInfo entity : page.getResult()){
				ExcelInfoVO vo =new ExcelInfoVO();
				vo.setId(entity.getId());
				vo.setCode(entity.getCode());
				vo.setDateType(entity.getDateType());
				vo.setName(entity.getName());
				vo.setPath(entity.getPath());
				vo.setUploadDate(entity.getUploadDate());
				listvo.add(vo);
			}
		}
		
		int start = (pageNo - 1)*pageSize;
		return new Page<ExcelInfoVO>(start, page.getTotalCount(), pageSize, listvo);
	}

	@Override
	public List<ExcelInfoVO> findByTime(String date) {
		List<ExcelInfo> listEntity = new ArrayList<ExcelInfo>();
		List<ExcelInfoVO> listVO = new ArrayList<ExcelInfoVO>();
		ExcelInfo excelInfo =new ExcelInfo();
		listEntity =excelInfo.findByTime(date);
		if(listEntity!=null && listEntity.size()>0){
			for(ExcelInfo entity :listEntity){
				ExcelInfoVO vo =new ExcelInfoVO();
				vo.setId(entity.getId());
				vo.setCode(entity.getCode());
				vo.setDateType(entity.getDateType());
				vo.setName(entity.getName());
				vo.setPath(entity.getPath());
				vo.setUploadDate(entity.getUploadDate());
				listVO.add(vo);
			}
		}
	
		return listVO;
	}

	@Override
	public List<ExcelInfoVO> findBycode(String code, String date) {
		List<ExcelInfo> listEntity = new ArrayList<ExcelInfo>();
		List<ExcelInfoVO> listVO = new ArrayList<ExcelInfoVO>();
		ExcelInfo excelInfo =new ExcelInfo();
		listEntity =excelInfo.findBycode(code, date);
		if(listEntity!=null && listEntity.size()>0){
			for(ExcelInfo entity :listEntity){
				ExcelInfoVO vo =new ExcelInfoVO();
				vo.setId(entity.getId());
				vo.setCode(entity.getCode());
				vo.setDateType(entity.getDateType());
				vo.setName(entity.getName());
				vo.setPath(entity.getPath());
				vo.setUploadDate(entity.getUploadDate());
				listVO.add(vo);
			}
		}
	
		return listVO;
	}

	@Override
	public List<ExcelInfoVO> findBycodeAndTime(String code, String date) {
		List<ExcelInfo> listEntity = new ArrayList<ExcelInfo>();
		List<ExcelInfoVO> listVO = new ArrayList<ExcelInfoVO>();
		ExcelInfo excelInfo =new ExcelInfo();
		listEntity =excelInfo.findBycodeAndTime(code, date);
		if(listEntity!=null && listEntity.size()>0){
			for(ExcelInfo entity :listEntity){
				ExcelInfoVO vo =new ExcelInfoVO();
				vo.setId(entity.getId());
				vo.setCode(entity.getCode());
				vo.setDateType(entity.getDateType());
				vo.setName(entity.getName());
				vo.setPath(entity.getPath());
				vo.setUploadDate(entity.getUploadDate());
				listVO.add(vo);
			}
		}
	
		return listVO;
	}
	

}
